<?php
/**
 * Created by PhpStorm.
 * User: w-pc
 * Date: 2017/03/13
 * Time: 17:03
 */
namespace Admin\Controller;
use Think\Controller;
class DiaryController extends CommonController {
    public function index(){
        $d_state = request('get','int','d_state');
        $page = request('get','int','page',1);
        if($d_state==0){
            $diary_list = D('Diary')->get_DiaryList(0);
        }else{
            $diary_list = D('Diary')->get_DiaryList($d_state);
        }
        $page_list = pageKeyList($page,'diary','d_state',$d_state,$diary_list);
        $diary_list = pageList($page,$diary_list);
        $this->assign('page_list',$page_list);
        $this->assign('diary_list',$diary_list);
        $this->display();
    }
    public function show(){
        $d_id = request('get','int','d_id');
//        echo $d_id;exit();
        if($d_id!=0){
            $diary = D('Diary')->get_DiaryByOne($d_id);
        }else{
            $diary = array();
        }
//        var_dump($diary);exit();
        $this->assign('diary',$diary);
        $this->display();
    }
    public function stick(){
        $url = $_SERVER['HTTP_REFERER'];
        $d_id = request('get','int','d_id');
        D('Diary')->set_StickState($d_id);
        $this->redirect($url);
    }
    public function upload(){
        $d_id = request('post','int','d_id');
        if($d_id==0){
            $d_date = request('post','str','d_date');
            $d_content = request('post','str','d_content');
            $arr['d_date'] = $d_date;
            $arr['d_content'] = $d_content;
            $d_id = D('Diary')->save_Diary($arr);
        }else{
            $d_date = request('post','str','d_date');
            $d_content = request('post','str','d_content');
            $arr['d_date'] = $d_date;
            $arr['d_content'] = $d_content;
            D('Diary')->update_Diary($d_id,$arr);
        }
        $this->redirect('/admin.php?c=diary&a=show&d_id='.$d_id);
    }
    public function delete(){
        $d_id = request('post','int','d_id');
        if($d_id!=0){
            D('Diary')->delete($d_id);
        }
    }
}